package com.zhf.servlet.student;

import com.zhf.entity.Page;
import com.zhf.entity.Student;
import com.zhf.service.StudentService;
import com.zhf.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "StudentFindServlet", urlPatterns = "/studentFind")
public class StudentFindServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String queryCode = request.getParameter("queryCode");
        String queryName = request.getParameter("queryName");
        List<Student> studentList = studentService.queryStudentByCodeAndName(queryCode, queryName);
        if(studentList == null) studentList = new ArrayList<>();
        request.setAttribute("students", studentList);
        request.setAttribute("displayPageComponent", "none");
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
